#ifndef SWP_SWIMLANE_GRAPHIC_ITEM_H
#define SWP_SWIMLANE_GRAPHIC_ITEM_H

#include "abstractpolygongraphicitem.h"

#include <ellipsegraphicitem.h>
#include <pathgraphicitem.h>

namespace swp {

class SwpSwimlaneGraphicItem : public AbstractPolygonGraphicItem
{
    class ResizeHandle : public gui::EllipseGraphicItem
    {
    public:
        ResizeHandle(SwpSwimlaneGraphicItem* parent);

        void setIgnoreFlag(bool flag);
        void initPos();

    protected:
        QVariant itemChange(GraphicsItemChange change, const QVariant& value);

        void mousePressEvent(QGraphicsSceneMouseEvent *event);
        void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

    private:
        SwpSwimlaneGraphicItem* m_parent;
        QPolygonF m_polygonBeforeChange;
        gui::PolygonGraphicItem* m_symbol;
        bool m_ignoreFlag;
        bool m_posInited;
    };

    class SwitchHandle : public gui::EllipseGraphicItem
    {
    public:
        SwitchHandle(SwpSwimlaneGraphicItem* parent);

    protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *event);

    private:
        SwpSwimlaneGraphicItem* m_parent;
        gui::PathGraphicItem* m_symbol;
    };

public:
    SwpSwimlaneGraphicItem(const QString& name = QString(),
        QGraphicsItem* parent = 0);
    virtual ~SwpSwimlaneGraphicItem();

    SwpSwimlaneGraphicItem* previous() const;
    SwpSwimlaneGraphicItem* next() const;

    // AbstractPolygonGraphicItem
    void setPolygonF(const QPolygonF& polygon);

    // IEditorItem
    virtual bool supportsConnection(
        const core::ConnectionData& connection) const;

    virtual int type() const;

    QString name() const;

    virtual IEditorItem* copy() const;

    // IPropertyObject
    virtual QList<core::Property> properties() const;

    virtual QVariant propertyValue(const QString& propertyName) const;

    virtual void propertyChanged(const QString& property,
        const QVariant& newValue, bool silent = false);

    virtual QVariantList optionList(const QString& propertyName) const;
    virtual QVariantList autocomplete(const QString& propertyName) const;

    QRectF boundingRectWithoutHandles() const;

    void handleSceneResize();

    // QGraphicsPolygonItem
    QRectF boundingRect() const;
    QPainterPath shape() const;

protected:
    // AbstractPolygonGraphicItem
    QList<QPointF> scalePoints() const;
    QList<QPointF> connectPoints() const;
    QList<QPointF> selfConnectAnchorPoints() const;

    int paletteType() const;

    QVariant itemChange(GraphicsItemChange change,
        const QVariant& value);

    virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);

private:
    void setPrevious(SwpSwimlaneGraphicItem* previous);
    void setNext(SwpSwimlaneGraphicItem* next);

    void setIgnoreFlag(bool flag);

    void resizeWidthTo(qreal handleX);

protected:
    friend class SwpProcessEditor;
    friend class AddSwimlaneCommand;
    friend class RemoveSwimlaneCommand;
    friend class SwapSwimlanesCommand;
    QString m_name;
    core::Property m_nameProp;
    qreal m_minWidth;
    ResizeHandle* m_resizeHandle;
    SwitchHandle* m_switchHandle;
    SwpSwimlaneGraphicItem* m_previous;
    SwpSwimlaneGraphicItem* m_next;
    bool m_ignoreFlag;
};

} // namespace swp

#endif
